<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ValidatorTest
 *
 * @author joona
 */
class Unit_Core_ValidatorTest extends Lib_UnitTestcase
{
    public function test_validateAgainst_typeTrue()
    {
        $rule = array('type' => 'integer');
        $value = 1337;
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_typeFalse()
    {
        $rule = array('type' => 'string');
        $value = 1337;
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_allowNull_true()
    {
        $rule = array('allowNull' => true);
        $value = null;
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));

    }

    public function test_validateAgainst_allowNull_false()
    {
        $rule = array('allowNull' => false);
        $value = null;
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_allowNull_falseFromUnset()
    {
        $rule = array();
        $value = null;
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_maxLength_ok()
    {
        $rule = array('maxLength' => 5);
        $value = 'fuck';
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }
    public function test_validateAgainst_maxLength_fail()
    {
        $rule = array('maxLength' => 5);
        $value = 'waytoolongstring';
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_minLength_ok()
    {
        $rule = array('minLength' => 5);
        $value = 'fuckyougeorgebush';
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }
    public function test_validateAgainst_minLength_fail()
    {
        $rule = array('minLength' => 15);
        $value = 'tooshort';
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_maxValue_ok()
    {
        $rule = array('maxValue' => 100);
        $value = 99;
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_maxValue_fail()
    {
        $rule = array('maxValue' => 100);
        $value = 101;
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_minValue_ok()
    {
        $rule = array('minValue' => 100);
        $value = 101;
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_minValue_fail()
    {
        $rule = array('minValue' => 100);
        $value = 99;
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_regexp_ok()
    {
        $rule = array('regexp' => '/^[A-Z]{4}$/');
        $value = 'ABBA';
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_regexp_fail()
    {
        $rule = array('regexp' => '/^[A-Z]{4}$/');
        $value = 'somethingtrulyinvalid';
        $this->assertFalse($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_multipeRules_string()
    {
        $rule = array(
            'type' => 'string',
            'minLength' => 5,
            'maxLength' => 10,
            'regexp' => '/^[A-Za-z]+$/'
        );
        $value = 'JustRight';
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_multipeRules_integer()
    {
        $rule = array(
            'type' => 'integer',
            'minValue' => 5,
            'maxValue' => 10
        );
        $value = 7;
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }

    public function test_validateAgainst_multipeRules_float()
    {
        $rule = array(
            'type' => 'double',
            'minValue' => 5.5,
            'maxValue' => 10.5
        );
        $value = 7.1235123;
        $this->assertTrue($this->__callStatic('Core_Validator::validateAgainst',
                array($value, $rule)));
    }
}

